package com.wst.system.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wst.system.config.exception.GlobalExcetion;
import com.wst.system.security.entity.SysRoleEntity;
import com.wst.system.security.entity.SysUserEntity;
import com.wst.system.security.entity.SysUserRoleEntity;
import com.wst.system.security.entity.dto.SysUserRoleEntityDto;
import com.wst.system.security.entity.vo.SysUserRoleEntityVo;
import com.wst.system.server.mapper.SysUserRoleMapper;
import com.wst.system.server.service.SysRoleService;
import com.wst.system.server.service.SysUserRoleService;
import com.wst.system.server.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:50
 * @since JDK 1.8
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRoleEntity> implements SysUserRoleService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public List<SysUserRoleEntity> selectUserRoleRoleEntity(String uid, String[] rids) {
       return super.getBaseMapper().selectSysUserRoleEntityList(uid,rids);
    }

    @Override
    public List<SysUserRoleEntity> selectUserRoleEntityByUid(Serializable uid) {
        LambdaQueryWrapper<SysUserRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRoleEntity ::getUserId,uid);
        List<SysUserRoleEntity> sysUserRoleEntityList=this.baseMapper.selectList(queryWrapper);
        if(sysUserRoleEntityList==null){
            return new ArrayList<>();
        }
        return sysUserRoleEntityList;
    }

    @Override
    public List<SysUserRoleEntityDto> selectUserRoleEntityDtoByUid(Serializable uid) {
        LambdaQueryWrapper<SysUserRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRoleEntity ::getUserId,uid);
        List<SysUserRoleEntity> sysUserRoleEntityList=this.baseMapper.selectList(queryWrapper);
        List<SysUserRoleEntityDto> sysUserRoleEntityDtoList=new ArrayList<>();
        if(sysUserRoleEntityList!=null){
            for(SysUserRoleEntity sysUserRoleEntity:sysUserRoleEntityList){
                SysUserRoleEntityDto sysUserRoleEntityDto=new SysUserRoleEntityDto();
                SysRoleEntity sysRoleEntity=sysRoleService.getById(sysUserRoleEntity.getRoleId());
                sysUserRoleEntityDto.setId(sysUserRoleEntity.getId());
                sysUserRoleEntityDto.setRoleId(sysUserRoleEntity.getRoleId());
                sysUserRoleEntityDto.setRoleName(sysRoleEntity.getRoleName());
                sysUserRoleEntityDto.setRoleNo(sysRoleEntity.getRoleno());
                sysUserRoleEntityDto.setUid(Integer.valueOf(uid.toString()));
                sysUserRoleEntityDtoList.add(sysUserRoleEntityDto);
            }
        }
        return sysUserRoleEntityDtoList;
    }

    @Override
    public void saveUserRole(SysUserRoleEntityVo vo) throws GlobalExcetion {
        SysUserEntity sysUserEntity=sysUserService.getById(vo.getUserId());
        if(sysUserEntity==null){
            throw new GlobalExcetion(1,"用户不存在");
        }
        List<SysUserRoleEntity> userRoleEntityList=this.selectUserRoleEntityByUid(vo.getUserId());
        List<Integer> roleIdList=userRoleEntityList.stream().map(SysUserRoleEntity::getRoleId).collect(Collectors.toList());
        List<SysRoleEntity> roleEntities=sysRoleService.getRoleByRolenos(vo.getRoleNos());
        if(roleEntities!=null && roleEntities.size()>0){
            //用户新添加的角色id
            List<Integer> newRoleIdList=roleIdList.stream().map(roleId->roleId).collect(Collectors.toList());
            List<Integer> filterRoleList=new ArrayList<>();
            //用户原本的角色id
            for(Integer roleId:roleIdList){
                if(!newRoleIdList.contains(roleId)){
                    //除掉没有选中的角色id
                    this.deleteByUserIdAndRoleId(sysUserEntity.getUserId(),roleId);
                }else {
                    filterRoleList.add(roleId);
                }
            }
            List<SysUserRoleEntity> sysUserRoleEntityList=new ArrayList<SysUserRoleEntity>();
            for(SysRoleEntity roleEntity:roleEntities){
                if(!filterRoleList.contains(roleEntity.getRoleId())){
                    SysUserRoleEntity sysUserRoleEntity=new SysUserRoleEntity();
                    sysUserRoleEntity.setUserId(sysUserEntity.getUserId());
                    sysUserRoleEntity.setRoleId(roleEntity.getRoleId());
                    sysUserRoleEntityList.add(sysUserRoleEntity);
                }
            }
            if(sysUserRoleEntityList.size()>0){
                this.saveBatch(sysUserRoleEntityList);
            }
        }
    }

    @Override
    public void deleteByUserIdAndRoleId(Serializable uid, Serializable rid) throws GlobalExcetion {
        LambdaQueryWrapper<SysUserRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRoleEntity ::getUserId,uid);
        queryWrapper.eq(SysUserRoleEntity ::getRoleId,rid);
        this.remove(queryWrapper);
    }
}
